// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import http from "./api/http";
import router from './router'
import ElementUI from 'element-ui';
import base64 from './api/base64'
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);
Vue.config.productionTip = false
Vue.prototype.$axios = http;

Vue.prototype.global = function () {
  // const token = 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6Nzc1LCJ1c2VybmFtZSI6IndodzY2NiIsInJlZ2lzdGVyX2F0IjoxNTQyMzMzMjI1ODY4LCJsYXN0X2xvZ2luX2F0IjoxNTQyMzM2OTY4NjEzLCJlbmFibGVkIjoxfQ.4gl7A3sZpJcarfhe2m6PqNxs9_Ywg6w-s0_ppXbFDzM'
  const token = sessionStorage.getItem('token');
  const pageSize = 10
  return {token,pageSize}
}
Vue.filter('baseName',function (num) {
  let base = new base64.Base64()
  return base.decode(num)
})
Vue.filter('fixed',function (num) {
  if(num && num>0){
    return parseFloat(num).toFixed(2)
  }else{
    return 0
  }
})
Vue.prototype.changeDate = function(time){
  var date = new Date(parseInt(time));
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  let D = date.getDate() + ' ';
  var h = date.getHours() + ':';
  var m = date.getMinutes() + ':';
  if (date.getMinutes()<10){
    m = '0'+m
  }
  var s = date.getSeconds();
  if (s<10){
    s = '0'+s
  }
  return Y+M+D+h+m+s;
}
Vue.prototype.GetQueryString = function(name)
{
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
  if(r!=null)return  unescape(r[2]); return null;
}
Vue.prototype.Todate1 = function(time, format) { //Fri Oct 31 18:00:00 UTC+0800 2008
  var t = new Date(time);
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i
  }
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'MM':
        return tf(t.getMonth() + 1);
        break;
      case 'mm':
        return '00';
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return '00';
        break;
      case 'ss':
        return '00';
        break;
    }
  })
}
Vue.prototype.Todate = function(time, format) { //Fri Oct 31 18:00:00 UTC+0800 2008
  var t = new Date(time);
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i
  }
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'MM':
        return tf(t.getMonth() + 1);
        break;
      case 'mm':
        return '59';
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return '23';
        break;
      case 'ss':
        return '59';
        break;
    }
  })
}
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    sessionStorage.clear();
  }
  let userId = sessionStorage.getItem('token');
  if (!userId && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
